<?php

    /* klient http do wykonywania brudnej roboty zwiazanej z logowaniem */

    class CHttpSSL
    {
	var $m_hSocket;
	
	var $m_iHttpVer;
	var $m_iHttpCode;
	
	var $m_aCookie;
	var $m_szContentType;
	var $m_iDate;
    
        static function &GetInstance()
	{
	    global $g_hObjects;
	    static $m_hApp = NULL;
						    
	    if(!isset($m_hApp))
	    {
	        $m_hApp       = new CHttpSSL();
	        $g_hObjects[] = $m_hApp;
		$m_hApp->OnInit();												    
	    }
																	
	    return $m_hApp;
	}
	
	function OnInit()
	{
	}
	
	function OnDestroy()
	{    
	}	
	
	
	function ConnPostSSL($szUrl,$aVars,$iTimeout = 20)
	{
	    $aUrl = @parse_url($szUrl);
		    
	    if($aUrl['host'] == '' ) { return false; }
		    
	    $aUrl['path'] = isset($aUrl['path']) ? $aUrl['path'] : '/';
	    $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : '443';
		    
		    
	    //convert our array to post request
	    $szVars = '';
		    
	    foreach($aVars as $szKey => &$szVal)
	    {
		$szVars .= urlencode($szKey).'='.urlencode($szVal).'&';
	    }
		    
	    $szVars   = substr($szVars,0,-1);	//wycinamy ostatni &
	    $iVarsLen = strlen($szVars);	//strlen po zmiennych
		    
	    //http request
	    $szHeader = "POST ".$aUrl['path']." HTTP/1.0\r\n";
	    $szHeader.= "Host: ".$aUrl['host']."\r\n";
	    $szHeader.= "User-Agent: gorilla.pl\r\n";
	    $szHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";
	    $szHeader.= "Content-Length: ".$iVarsLen."\r\n";
	    $szHeader.= "Connection: Close\r\n\r\n";
		    
	    $szQuery = $szHeader.$szVars."\r\n";
		    
	    //let's help fsockopen do the job
	    $szIp = @gethostbyname($aUrl['host']);
		    
	    //opening ssl connection
	    $hSocket = @fsockopen('ssl://'.$szIp,$aUrl['port'],$iErrNo,$szErrorStr,$iTimeout);
		    
	    if(!$hSocket)
	    {
		return false;
	    }
		    
	    fwrite($hSocket,$szQuery);
		    
	    $szOutput = '';
		    
	    while(!feof($hSocket)) 
		$szOutput .= @fgets($hSocket,8192);
		    
	    fclose($hSocket);
	    
	    	
	    $iHeaderEnd = strpos($szOutput,"\r\n\r\n");	    
	    $szHeader   = substr($szOutput,0,$iHeaderEnd);
	    $szSite     = substr($szOutput,$iHeaderEnd + 4);
	    $aHeader    = explode("\r\n",$szHeader);
	    
	    $szHttpPatt = 'HTTP';
	    $szHttpLen  = strlen($szHttpPatt);
	    
	    $szCookiePatt = 'Set-Cookie:';
	    $szCookieLen  = strlen($szCookiePatt);
	    
	    $szContentPatt = 'Content-Type:';
	    $szContentLen  = strlen($szContentPatt);
	    
	    $szDatePatt = 'Date:';
	    $szDateLen  = strlen($szDatePatt);
	
	    $szRedirPatt = 'Location: ';
	    $szRedirLen  = strlen($szRedirPatt);
	
	        	    
	    foreach($aHeader as &$szLine)
	    {
		if(strncmp($szLine,$szHttpPatt,$szHttpLen) == 0)
		{
		    list($szHttpVer,$iCode) = explode(' ',$szLine);
		    
		    $this->m_iHttpVer  = $szHttpVer;
		    $this->m_iHttpCode = $iCode;
		}	
	    
		if(strncmp($szLine,$szCookiePatt,$szCookieLen) == 0)
		{
		    $szCookie = substr($szLine,$szCookieLen);
		    $szCookie = substr($szCookie,0,strpos($szCookie,';'));
		
		    list($szName,$szVal) = explode('=',$szCookie);    
		    
		    $szName = trim($szName);
		    $szVal  = trim($szVal);
		    
		    $this->m_aCookie[$szName] = $szVal;
		}
		
		if(strncmp($szLine,$szContentPatt,$szContentLen) == 0)
		{
		    $this->m_szContentType = substr($szLine,$szContentLen);
		    $this->m_szContentType = trim($this->m_szContentType);
		}
		
		if(strncmp($szLine,$szDatePatt,$szDateLen) == 0)
		{
		    $this->m_iDate = substr($szLine,$szDateLen);
		    $this->m_iDate = strtotime($this->m_iDate);
		}
		
		
		if(strncmp($szLine,$szRedirPatt,$szRedirLen) == 0)
		{
		    $this->m_szRedirUrl = substr($szLine,$szRedirLen);
		    $this->m_szRedirUrl = trim($this->m_szRedirUrl);
		}
	    }
	    
	    	    
	    return trim($szSite);
	}
		
	//czy mamy przekierownie
	function IsRedirect()
	{
	    return ($this->m_iHttpCode == 302) ?  true : false;
	}
	
	//adres przekierowania
	function GetRedirectUrl()
	{
	    return $this->m_szRedirUrl;
	}
	
	function GetCookie($szName)
	{
	    return isset($this->m_aCookie[$szName]) ?  $this->m_aCookie[$szName] : '';
	}

    }
?>